<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/18
 * Time: 20:44
 */
namespace Home\Controller;

use Think\Controller;

class NewsController extends BaseController
{
    //新闻主页
    public function index()
    {
        $option_id = I('get.id');
        $type = I('get.type', 1);

        $Option = D('Option');
        $option = $Option->where(array(
            'id' => array('eq',$option_id),
            'is_show' => array('neq',0),
            'navigator_id' => array('eq',7)
        ))->find();
        if (empty($option)) {
            $this->_empty();
        }
        $option['description'] = htmlspecialchars_decode($option['description']);

        $where['is_enabled'] = array('eq', 1);
        if (!empty($type)) {
            $where['type'] =array('eq', $type);
        }

        $New = D('News');
        //查询满足要求的总记录数
        $count = $New->where($where)->count();
        //实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page = new \Think\Page($count,10);
        $Page->setConfig('next',C('PAGE_CONFIG.next'));
        $Page->setConfig('prev',C('PAGE_CONFIG.prev'));
        $Page->setConfig('last',C('PAGE_CONFIG.last'));
        $Page->setConfig('first',C('PAGE_CONFIG.first'));
        $Page->lastSuffix=C('PAGE_CONFIG.lastSuffix');//在此设置一行
        $show = $Page->show();// 分页显示输出
        //进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $news = $New->where($where)->order('pub_time desc,sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($news as $key => $new) {
            $news[$key]['content'] = msubstr(strip_tags(htmlspecialchars_decode($new['content'])), 0, 80);
        }

        $this->assign(array(
            'type' => $type,
            // 赋值数据集
            'news' => $news,
            // 赋值分页输出
            'show' => $show,
            'nav'  => $option
        ));

       $this->display('web/News/news');
    }

    //新闻详情页
    public function detail()
    {
        $option_id = I('get.option');
        $type = I('get.type');
        $id = I('get.id');
        $New = D('News');
        $count = $New->where(array('id'=>array('eq',$id)))->count();
        if (!$count) {
            $this->_empty();
            exit;
        }
        $new = $New->where(array('id'=>array('eq',$id)))->find();
        $new['content'] = htmlspecialchars_decode($new['content']);
        $this->assign(array(
            'option_id' => $option_id,
            'new'   => $new,
            'type'  => $type,
        ));
        if ($this->isMobile()) {
            $this->display('Mobile/News/article');
        } else {
            $this->display('web/News/article');
        }
    }

}